package com.wc.算法基础课.E第五讲动态规划.数位统计DP.度的数量;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/17 22:24
 * @description [蓝桥杯 2021 国 BC] 二进制问题
 * https://www.luogu.com.cn/problem/P8764
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 65;
    // f[i][j] 表示i个数中选j个的方案
    static long[][] f = new long[N][N];
    static long n;
    static int k, b;

    public static void main(String[] args) {
        n = sc.nextLong();
        k = sc.nextInt();
        b = 2;
        long l = 1;
        long r = n;
        init();
        out.println(dp(r) - dp(l - 1));
        out.flush();
    }

    static long dp(long n) {
        if (n == 0) return 0;
        int[] nums = new int[N];
        int len = 0;
        long res = 0;
        while (n > 0) {
            nums[len++] = (int) (n % b);
            n /= b;
        }
        // 表示前面1的个数
        int last = 0;
        for (int i = len - 1; i >= 0; i--) {
            int x = nums[i];
            if (x > 0) {
                res += f[i][k - last];
                if (x > 1) {
                    // 这样右边的an-1就大于1了，不需要计算了
                    if (k - last - 1 >= 0) res += f[i][k - last - 1];
                    // 因为右边的位数只能是1不能是其他的数字
                    break;
                } else {
                    last++;
                    // 前面已经有了k个1了，就不需要计算了
                    if (last > k) break;
                }
            }
            // 前面有K个1，后面满足要求了
            if (i == 0 && last == k) res++;
        }

        return res;
    }

    static void init() {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j <= i; j++) {
                if (j == 0) f[i][j] = 1;
                else f[i][j] = f[i - 1][j] + f[i - 1][j - 1];
            }
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}